package com.udaos.modules.train.services;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;

import com.udaos.common.base.Service;
import com.udaos.models.region.Ud_region;
import com.udaos.models.sys.Sys_user;
import com.udaos.models.train.Ud_trainlump_product;
import com.udaos.modules.company.services.UdCompanyRegionService;

/**
 * @author mutaotao
 * @time 2017-03-07 15:15:46
 * 
 */
@IocBean(args = { "refer:dao" })
public class UdTrainlumpProductService extends Service<Ud_trainlump_product> {
	@Inject
	private UdCompanyRegionService companyRegionService;
	public UdTrainlumpProductService(Dao dao) {
		super(dao);
	}
	/**
	 * 获取主体下开通的片区
	 * @return
	 */
	public List<Ud_region> getRegions() {
		Subject subject = SecurityUtils.getSubject();
		Sys_user user = (Sys_user) subject.getPrincipal();
		String company_id=user.getUnit().getCompany_id();
		return companyRegionService.findRegionById(company_id);
	}
	/**
	 * 初始化数据
	 * @param length
	 * @param start
	 * @param draw
	 * @param name
	 * @return
	 */
	public NutMap init(int length, int start, int draw, String name) {
		String sqls="SELECT tp.id AS id,c.name AS company_name,g.name AS region_name,tp.name AS name, tp.code AS code,p.name AS project_name,tp.train_mode AS train_mode,tp.shuttle_mode AS shuttle_mode,tp.specs AS specs,tp.price AS price,tp.payment AS payment,tp.remark AS remark,tp.state AS state"
					+" FROM ud_trainlump_product tp,ud_company  c,ud_region g,ud_train_project p"
					+" WHERE tp.company_id=c.id AND tp.region_id=g.id AND tp.project_id =p.id AND tp.delFlag=FALSE";
		//获取主体
		Subject subject = SecurityUtils.getSubject();
		Sys_user user = (Sys_user) subject.getPrincipal();
		String company_id=user.getUnit().getCompany_id();
		sqls+=" AND tp.company_id='"+company_id+"'";
		if(!Strings.isBlank(name)){
			sqls+=" AND tp.name LIKE '%"+name+"%'";
		}
		
		Sql sql =Sqls.create(sqls);
		return this.data(length, start, draw, sql, sql);
	}
}